<?php

require_once '../../classes/includeAll_Layer2.php';
require_once '../../resources/getConnection.php';
if(isset($_POST["del"]))
{
  $del = $_POST["del"];
  foreach ($del as $key => $box) {
    if(is_numeric($key))
    {
      $delFlags = 0;
      $dev = new Device($key,$connection);
      foreach ($box as $value) {
        $delFlags |= $value;
      }
      
      $newFlag = $dev->ChangeFlag & (~$delFlags);
      $dev->UpdateDevice(array("changeFlag"), array($newFlag));
    }
  }
}

//echo new Table
$search = array ("-", "ae",     "ue",     "oe"    ,"Ae");
$replace = array(" ", "&auml;", "&uuml;", "&ouml;","&Auml;");

$devicesClass = new Devices($connection);
$devices = $devicesClass->getAllDevices();
$connection->disconnectDB();

$allNames = Array();
$allMessages = Array();
$allTypes = Array();

$flagTable = "";
$filterTable = "";

//flag Table
foreach ($devices as $device) {
  if($device->ChangeFlag>0)
  {
    $n=0;
    $currentType=$device->DeviceType;

    if(!in_array($currentType, $allTypes))
      array_push($allTypes, $currentType);

    if($device->Name == "")
      $currentName="-";
    else
      $currentName=$device->Name;

    if(!in_array($currentName, $allNames))
      array_push($allNames, $currentName);

    foreach ((new allFlags)->DeviceFlags as $key => $value) {
      if(($value & $device->ChangeFlag)>0)
      {
        $currentMessage =str_replace($search, $replace, $key);
        if(!in_array($currentMessage, $allMessages))
          array_push ($allMessages, $currentMessage);

        $flagTable.='<tr style="display: table-row;">'.
                      '<td><input type="checkbox" name="del['.$device->ID.']['.$n.']" value="'.$value.'" class="checkDel" /></td>'.
                      '<td>'.alertLevel($key).'</td>'.
                      '<td><a href="device.php?dev='. $device->ID.'" >'. $currentMessage .'</td>'.
                      '<td><a href="device.php?dev='. $device->ID.'" >'. $device->DeviceType.'</a></td>'.
                      '<td><a href="device.php?dev='. $device->ID.'" >'.$currentName.'</a></td>'.
                    '</tr>';
        $n++;
      }
    }      
  }
}
sort($allMessages);
sort($allTypes);
sort($allNames);
//filter Table
$filterTable.='<tr>
  <td colspan="2">
    <h4>Meldung:</h4>
    <input type="checkbox" name="filterAllNotice" value="" checked="checked" id="filterAllNotice"/> Alle ausw&auml;hlen
    <div class="filterRange">';

      foreach ($allMessages as $message) {
      $filterTable.= '<input type="checkbox" name="noteFilter" value="'.$message.'" checked="checked" class="noteFilter"/> '.$message.'<br>';
      }

$filterTable.='</div></td>'
.'</tr>'
.'<tr>
  <td>
    <h4>Ger&auml;t:</h4>
    <select name="singleDev" id="devSelector">
      <option value="0">Alle Ger&auml;te</option>';
      foreach ($allNames as $name) {
        $filterTable.= '<option value="'.$name.'">'.$name.'</option>';
      } 
$filterTable.='</select></td>
    <td>
      <h4>Ger&auml;tetyp:</h4>
      <input type="checkbox" name="filterAllDevice" value="" checked="checked" id="filterAllDevice" />Alle ausw&auml;hlen
      <div class="filterRange">';

      foreach ($allTypes as $type) {
        $filterTable.= '<input type="checkbox" name="deviceFilter" value="'.$type.'" checked="checked" class="deviceFilter"/> '.$type.'<br>';
      }

$filterTable.='</div></td>
</tr>';

echo json_encode(Array("flags"=>$flagTable,"filter"=>$filterTable));
